package cn.gjculture.shopping.api.service.impl;

import cn.gjculture.shopping.api.dao.*;
import cn.gjculture.shopping.api.entity.Art;
import cn.gjculture.shopping.api.entity.Commodity;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.ArtService;
import cn.gjculture.shopping.api.service.FileService;
import cn.gjculture.shopping.api.service.GoodsService;
import cn.gjculture.shopping.api.service.IdService;
import cn.gjculture.shopping.api.util.DateUtil;
import cn.gjculture.shopping.api.util.UUIDUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ArtServiceImpl implements ArtService {

    @Autowired
    ArtDao artDao;

    @Autowired
    CommonUpdateMapper commonUpdateMapper;

    @Autowired
    CommonDeleteMapper commonDeleteMapper;

    @Autowired
    GoodsDao goodsDao;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    IdService idService;

    @Autowired
    FileService fileService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    ArtMapper artMapper;



    @Override
    public int insert(Art art) {
        art.setGoodsId(UUIDUtil.getUUID());
        art.setInsertTime(DateUtil.getTimestamp());
        art.setUpdateTime(DateUtil.getTimestamp());
        art.setStatus(1);
        art.setRecommended(1);
        this.insertThumbnail(art.getThumbnails(), art.getGoodsId());
        return artDao.insert(art);
    }

    private int insertThumbnail(List<Commodity.Thumbnail> thumbnails, String goodsId) {
        if (thumbnails != null && thumbnails.size() > 0) {
            for (Commodity.Thumbnail tb : thumbnails) {
                tb.setGoodsId(goodsId);
            }
            goodsMapper.insertThumbnail(thumbnails);
            return thumbnails.size();
        }
        return 0;
    }


    @Override
    public int update(Art art) {
        art.setUpdateTime(DateUtil.getTimestamp());
        goodsService.handleThumb(art.getGoodsId(), art.getThumbnails());
        return commonUpdateMapper.updateArt(art);
    }



    @Override
    public List<Art> query(Art art) {
        List<Art> arts=artMapper.query(art);
        if(!CollectionUtils.isEmpty(arts)){
            for (Art a:arts){
                collect(a);
            }
        }
        return arts;
    }

    private void collect(Art a) {
        if(a==null) return ;
        a.setThumbnails(goodsDao.queryThumb(a.getGoodsId()));
    }

    @Override
    public Art queryById(String artId) {
        return artDao.queryById(artId);
    }

    @Override
    public int delete(String id, Return ret) {
        return artDao.delete(id);
    }


    @Override
    public int updateThumbnail(String thumbnails, String goodsId) {
        List<Commodity.Thumbnail> thumbnailList=JSONObject.parseObject(thumbnails,new TypeReference<List<Commodity.Thumbnail>>(){});
        goodsDao.deleteThembnail(goodsId);
        if(!CollectionUtils.isEmpty(thumbnailList)){
            goodsMapper.insertThumbnail(thumbnailList);
            return thumbnailList.size();
        }
        return 0;
    }

    @Override
    public List<Commodity.Thumbnail> queryThumbnail(String goodsId) {
        return goodsDao.queryThumb(goodsId);
    }

    @Override
    public int updateOther(Art art) {
        art.setUpdateTime(DateUtil.getTimestamp());
        return commonUpdateMapper.updateArt(art);
    }
}
